package cn.xinfei.xdecision.manager.controller.component.scorecard;

import cn.xinfei.xdecision.common.model.common.PageResult;
import cn.xinfei.xdecision.common.model.common.ResponseEntityBuilder;
import cn.xinfei.xdecision.common.model.common.ResponseEntityDto;
import cn.xinfei.xdecision.common.model.common.requestParam.UpdateStatusParam;
import cn.xinfei.xdecision.common.model.component.CopyToFolderReqVo;
import cn.xinfei.xdecision.common.model.component.request.ListParam;
import cn.xinfei.xdecision.common.model.component.scorecard.Scorecard;
import cn.xinfei.xdecision.common.model.component.scorecard.vo.ScorecardVo;
import cn.xinfei.xdecision.common.service.component.scorecard.ScorecardService;
import cn.xinfei.xdecision.common.utils.constant.OperateNameConst;
import cn.xinfei.xdecision.common.utils.constant.OperateTypeEnum;
import cn.xinfei.xdecision.common.utils.enums.enginex.ComponentStatusEnum;
import cn.xinfei.xdecision.common.utils.exception.ApiException;
import cn.xinfei.xdecision.common.utils.exception.enums.ErrorCodeEnum;
import cn.xinfei.xdecision.common.utils.util.strategyx.SectionUtils;
import cn.xinfei.xdecision.manager.web.aop.ArchivesLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "组件中心-评分卡")
@RestController
@RequestMapping("/v3/scorecard/")
public class ScorecardController {

    @Resource
    private ScorecardService scorecardService;

    @ApiOperation(value = "评分卡_规则详情")
    @PostMapping(value = "getScorecardInfo/{id}")
    public ResponseEntityDto<ScorecardVo> getScorecardInfo(@PathVariable Long id) {
        if (id == null) {
            throw new ApiException(ErrorCodeEnum.PARAMS_EXCEPTION.getCode(), ErrorCodeEnum.PARAMS_EXCEPTION.getMessage());
        }
        return ResponseEntityDto.success(scorecardService.queryById(id));
    }

    @ApiOperation(value = "评分卡_规则列表")
    @PostMapping(value = "getScorecardList")
    public ResponseEntityDto<PageResult<List<ScorecardVo>, Scorecard>> getScorecardList(@RequestBody ListParam listParam) {
        return scorecardService.queryByEntity(listParam);
    }

    @ApiOperation(value = "评分卡_规则启用")
    @PostMapping(value = "enableScorecard")
    @ArchivesLog(type = OperateTypeEnum.UPDATE, name = OperateNameConst.ComponentOpt.ENABLE)
    public ResponseEntityDto<String> enableScorecard(@RequestBody UpdateStatusParam param) {
        scorecardService.updateScorecard(
                param.getList(),
                ComponentStatusEnum.ENABLE.getStatus(), "当前规则状态是" + ComponentStatusEnum.ENABLE.getName());
        return ResponseEntityDto.success("评分卡规则启用成功!");
    }

    @ApiOperation(value = "评分卡_规则停用")
    @PostMapping(value = "disableScorecard")
    @ArchivesLog(type = OperateTypeEnum.UPDATE, name = OperateNameConst.ComponentOpt.DISABLE)
    public ResponseEntityDto<String> disableScorecard(@RequestBody UpdateStatusParam param) {
        scorecardService.updateScorecard(
                param.getList(),
                ComponentStatusEnum.DISABLE.getStatus(),
                "当前规则状态是" + ComponentStatusEnum.DISABLE.getName());
        return ResponseEntityDto.success("评分卡规则停用成功!");
    }

    @ApiOperation(value = "评分卡_保存规则及版本")
    @PostMapping(value = "saveScorecard")
    @ArchivesLog(type = OperateTypeEnum.CREATE, name = OperateNameConst.ComponentOpt.SAVE)
    public ResponseEntityDto<Long> checkAndSaveScorecard(@RequestBody ScorecardVo scorecardVo) {
        return ResponseEntityDto.success("保存评分卡规则成功!", scorecardService.checkAndSaveScorecard(scorecardVo));
    }

    @ApiOperation(value = "评分卡_复制规则及全部版本")
    @PostMapping(value = "copyScorecard")
    @ArchivesLog(type = OperateTypeEnum.CREATE, name = OperateNameConst.ComponentOpt.COPY)
    public ResponseEntityDto<ScorecardVo> copyScorecard(@RequestBody CopyToFolderReqVo req) {
        scorecardService.copyScorecard(req);
        return ResponseEntityDto.success("复制评分卡成功!");
    }

    /**
     * @api {POST} /v2/folder/scorecard/section 5.07. 区间校验
     * @apiGroup scorecard
     * @apiVersion 2.0.0
     * @apiSuccess {String} status 状态: 1成功, 0失败
     * @apiSuccess {String} data.result 1:区间有效、-1:区间不完整或有重叠
     * @apiParamExample {json} 请求示例：
     * ["(,1]","(1,6]","(6,15]","(15,20]","(20,)"]
     * @apiSuccessExample {json} 成功返回数据示例：
     * {"status":"1","error":"00000000","msg":null,"data":{"result":"1","msg":"区间有效!"}}
     */
    @RequestMapping(value = "/section", method = RequestMethod.POST)
    public ResponseEntityDto<Object> saveEngineNode(@RequestBody List<String> sections) {

        // @RequestParam("sections[]") List<String> sections
        Map<String, String> resultMap = new HashMap<String, String>();
        // 验证区间完整性
        if (SectionUtils.checkSectionValid(sections)) {
            if (SectionUtils.checkSectionCoincide(sections)) {
                resultMap.put("result", "-1");
                resultMap.put("msg", "区间有重叠,请核准!");
            } else {
                resultMap.put("result", "1");
                resultMap.put("msg", "区间有效!");
            }
        } else {
            resultMap.put("result", "-1");
            resultMap.put("msg", "区间不完整,请核准!");
        }
        return ResponseEntityBuilder.buildNormalResponse(resultMap);
    }

}
